package org.sasha.roulette.chart;

import java.util.LinkedHashSet;

import javax.inject.Inject;

import com.github.peholmst.mvp4vaadin.AbstractView;
import com.invient.vaadin.charts.InvientCharts;
import com.invient.vaadin.charts.InvientCharts.Series;
import com.invient.vaadin.charts.InvientChartsConfig;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.themes.ChameleonTheme;

@SuppressWarnings("serial")
public class ChartViewImpl extends AbstractView<ChartView, ChartPresenter> implements ChartView {
    
    private InvientCharts occurenceChart;
    
    private InvientCharts parityChart;
    
    private InvientCharts thirdsChart;
    
    private HorizontalLayout auxChartWrapper = new HorizontalLayout();
    
    private VerticalSplitPanel chartWrapper = new VerticalSplitPanel();
    
    @Inject
    public ChartViewImpl(final ChartPresenter presenter) {
        super(false);
        setPresenter(presenter);
        presenter.setView(this);
        construct();
        init();
    }

    private void construct() {
        constructOccurenceChart();
        constructParityChart();
        constructThirdsChart();
        
        auxChartWrapper.setSizeFull();
        
        chartWrapper.setLocked(true);
        chartWrapper.addStyleName(ChameleonTheme.SPLITPANEL_SMALL);
        chartWrapper.setSplitPosition(70);
        
        chartWrapper.setSecondComponent(auxChartWrapper);
    }

    private void constructThirdsChart() {
        final InvientChartsConfig cfg = getPresenter().configureThirdsChart();
        thirdsChart = new InvientCharts(cfg);
        thirdsChart.setSizeFull();
        final CssLayout cl = new CssLayout();
        cl.setSizeFull();
        cl.addComponent(thirdsChart);
        auxChartWrapper.addComponent(cl);        
    }

    private void constructParityChart() {
        final InvientChartsConfig cfg = getPresenter().configureParityChart();
        parityChart = new InvientCharts(cfg);
        parityChart.setSizeFull();
        final CssLayout cl = new CssLayout();
        cl.setSizeFull();
        cl.addComponent(parityChart);
        auxChartWrapper.addComponent(cl);
    }

    private void constructOccurenceChart() {
        final InvientChartsConfig cfg = getPresenter().configureOccurenceChart();
        occurenceChart = new InvientCharts(cfg);
        occurenceChart.setSizeFull();
        chartWrapper.setFirstComponent(occurenceChart);
    }

    @Override
    public ComponentContainer getViewComponent() {
        return chartWrapper;
    }

    @Override
    public void refresh() {
        final LinkedHashSet<Series> occurenceSeries = getPresenter().getOccurenceSeries();
        final LinkedHashSet<Series> paritySeries = getPresenter().getParitySeries();
        final LinkedHashSet<Series> thirdsSeries = getPresenter().getThirdsSeries();
        occurenceChart.setSeries(occurenceSeries);
        parityChart.setSeries(paritySeries);
        thirdsChart.setSeries(thirdsSeries);
    }
   
}
